﻿const router = require("koa-router")()
const KeyaccountModel = require('../models/keyaccount')
const AccountMOdel = require('../models/keyaccount')
const OrderModel = require('../models/order')
const orderModel = require('../models/order')
const partner = require("../models/partner")
const PartnerModel = require('../models/partner')
const activityModle = require('../models/activity')
const { updata, updata1, areaData } = require('../config/updata')


const screenArray = require('../config/screenArray')
const randomNumber = require('../config/randomNumber')


router.prefix("/api")

//查询所有大客户
router.get('/getAllKeyAccounts', async (ctx) => {
    ctx.status = 200

    let accountData = await KeyaccountModel.find(
        {}         
    )

    ctx.body = {
        code: 200,
        data: accountData
    }
})

//添加大客户
router.post("/Addkeyaccount", async (ctx) => {
    ctx.status = 200

    let data = ctx.request.body

    let addAccount = await KeyaccountModel.create(data)

    ctx.body = {
        code: 200,
        msg: "添加成功"
    }
})

//修改大客户状态
router.post("/setAccountsStatus", async (ctx) => {
    ctx.status = 200

    let { _id } = ctx.request.body

    let account = await KeyaccountModel.findOne({ _id })

    let Account_status = !account.Account_status

    let res = await KeyaccountModel.findOneAndUpdate({ _id }, { $set: { Account_status } })

    if (Account_status == 1) {
        ctx.body = {
            code: 200,
            msg: '解除禁用'
        }
    } else {
        ctx.body = {
            code: 200,
            msg: '账号禁用'
        }
    }
})

//大客户专员列表查询功能
router.post("/queryAllKeyAccounts", async (ctx) => {
    ctx.status = 200

    let queryData = ctx.request.body

    let res = await KeyaccountModel.find(queryData)

    ctx.body = {
        code: 200,
        data: res
    }
})

//查询该大客户的销售信息，订单
router.post('/getAccountInfo',async (ctx)=>{
    ctx.status = 200

    let {_id} = ctx.request.body

    let res_acc = await KeyaccountModel.findOne({_id})

    let name = res_acc.Sales_name

    let res_order = await OrderModel.find({name})

    ctx.body = {
        code:200,
        data: {
            keyAccount:res_acc,
            orderList:res_order
        }
    }
})



//-----------------------------------------------------------------------
// 总数据统计页面接口
// 查询所有用户数据
router.get("/getCustomerData", async (ctx) => {


    let keyaccounts = await AccountMOdel.find({});
    console.log(keyaccounts);
    let order = await orderModel.find({})
    let Partner = await PartnerModel.find({})
    console.log(Partner);
    // 总销售数量
    let res = 0
    Partner.forEach(item => {
        console.log(1111, item.sales_volume);
        res += item.sales_volume
    })
    let res1 = 0
    keyaccounts.forEach(item => {
        console.log(1111, item.sales_number);
        res1 += item.sales_number
    })
    //总注册人数
    let korder = keyaccounts.length + Partner.length
    //总成交数量
    let res2 = 0
    order.forEach(item => {
        console.log(item);
        if (item.order_status == 1) {
            res2 += item.order_status
        }
    })
    ctx.body = {
        code: 200,
        msg: "查询成功",
        data: {
            res2: res2,
            korder: korder,
            res: res + res1,
        }
    }

})

// 查询省市区总数据
router.post("/queryCustomerData", async (ctx) => {
    let data = ctx.request.body

    let {
        datas,
        create_time
    } = data
    if (!datas) {
        return ctx.body = {
            code: 400,
            msg: "查询失败"
        }
    }
    let keyaccounts = await AccountMOdel.find(datas);
    console.log(3, keyaccounts);
    let order = await orderModel.find(datas)
    console.log(222, order);
    let Partner = await PartnerModel.find(datas)
    console.log(Partner);
    let arr = {
        startTime: 1615367375165,
        endTime: Date.now().valueOf(),

    }
    if (create_time) {
        // 总成交用户数量
        let newArr = screenArray(order, 'create_time', create_time)
        // 总用户注册统计
        let newArr1 = screenArray(keyaccounts, 'timer', create_time)
        let newArr2 = screenArray(Partner, 'timer', create_time)
        let res = newArr1.length + newArr2.length
        // 用户总销量
        let arr1 = CalculateQuantity1(newArr1)

        let arr2 = CalculateQuantity2(newArr2)
        let userNum = arr1 + arr2
        //总成交数量
        let res2 = 0
        newArr.forEach(item => {
            console.log(item);
            if (item.order_status == 1) {
                res2 += item.order_status
            }
        })

        ctx.body = {
            code: 200,
            data: {
                newArr: newArr.length,
                res: res,
                userNum: userNum
            }
        }
    } else {
        let arr1 = CalculateQuantity1(keyaccounts)

        let arr2 = CalculateQuantity2(Partner)
        let userNum = arr1 + arr2
        let res = keyaccounts.length + Partner.length
        //总成交数量
        let res2 = []
        order.forEach(item => {
            console.log(item);
            if (item.order_status == 1) {
                res2.push(item.order_status)
            }
        })
        ctx.body = {
            code: 200,
            data: {
                order: res2.length,
                res: res,
                userNum: userNum
            }
        }
    }


})

// 省数据统计页面接口\
// 获取所有省数据
router.get("/getCopartnerData", async (ctx) => {
    let counts = []
    let partners = []
    let korder = await orderModel.find({});
    korder.filter(item => {
        if (item.type == "大客户") {
            let obj = {
                province: item.province,
                city: item.city,
                area: item.area,
                sale: item.order_quantity
            }
            let sta = false
            counts.forEach(res => {
                if (res.province == item.province && res.city == item.city && res.area == item.area) {
                    sta = true
                    if (item.order_status == 1) {
                        res.sale += item.order_quantity
                    }
                }
            })
            if (sta == false) {
                counts.push(obj)
            }
        } else {
            let obj = {
                province: item.province,
                city: item.city,
                area: item.area,
                sale: item.order_quantity
            }
            let sta = false
            partners.forEach(res => {
                if (res.province == item.province && res.city == item.city && res.area == item.area) {
                    sta = true
                    if (item.order_status == 1) {
                        res.sale += item.order_quantity
                    }
                }
            })
            if (sta == false) {
                partners.push(obj)
            }
        }
    })


    ctx.body = {
        code: 200,
        counts: counts,
        partners: partners


    }


})


// 通过省和时间查询省数据
router.post("/queryProvincialData", async (ctx) => {

    let {
        datas,
        create_time
    } = ctx.request.body
    if (!datas) {
        return ctx.body = {
            code: 400,
            msg: "查询失败,参数错误"
        }
    }
    let korder = await orderModel.find(datas);
    if (create_time) {
        let counts = []
        let partners = []
        let newArr1 = screenArray(korder, 'create_time', create_time)
        // 用户总销量
        newArr1.filter(item => {
            if (item.type == "大客户") {
                let obj = {
                    province: item.province,
                    city: item.city,
                    area: item.area,
                    sale: item.order_quantity
                }
                let sta = false
                counts.forEach(res => {
                    if (res.province == item.province && res.city == item.city && res.area == item.area) {
                        sta = true
                        if (item.order_status == 1) {
                            res.sale += item.order_quantity
                        }
                    }
                })
                if (sta == false) {
                    counts.push(obj)
                }
            } else {
                let obj = {
                    province: item.province,
                    city: item.city,
                    area: item.area,
                    sale: item.order_quantity
                }
                let sta = false
                partners.forEach(res => {
                    if (res.province == item.province && res.city == item.city && res.area == item.area) {
                        sta = true
                        if (item.order_status == 1) {
                            res.sale += item.order_quantity
                        }
                    }
                })
                if (sta == false) {
                    partners.push(obj)
                }
            }
        })
        ctx.body = {
            code: 200,
            counts: counts,
            partners: partners
        }
    } else {
        let counts = []
        let partners = []
        korder.filter(item => {
            if (item.type == "大客户") {
                let obj = {
                    province: item.province,
                    city: item.city,
                    area: item.area,
                    sale: item.order_quantity
                }
                let sta = false
                counts.forEach(res => {
                    if (res.province == item.province && res.city == item.city && res.area == item.area) {
                        sta = true
                        if (item.order_status == 1) {
                            res.sale += item.order_quantity
                        }
                    }
                })
                if (sta == false) {
                    counts.push(obj)
                }
            } else {
                let obj = {
                    province: item.province,
                    city: item.city,
                    area: item.area,
                    sale: item.order_quantity
                }
                let sta = false
                partners.forEach(res => {
                    if (res.province == item.province && res.city == item.city && res.area == item.area) {
                        sta = true
                        if (item.order_status == 1) {
                            res.sale += item.order_quantity
                        }
                    }
                })
                if (sta == false) {
                    partners.push(obj)
                }
            }
        })
        ctx.body = {
            code: 200,
            counts: counts,
            partners: partners
        }
    }
})
// 计算函数1
function CalculateQuantity1(newArr1) {
    let arr1 = 0
    newArr1.forEach(item => {
        arr1 += item.sales_number
    })
    return arr1
}
// 计算函数2
function CalculateQuantity2(newArr2) {
    let arr2 = 0
    newArr2.forEach(item => {
        arr2 += item.sales_volume
    })
    return arr2
}


//    创建活跃度表
router.post('/createHuoyuedu', async (ctx) => {

    let data = ctx.request.body
    let res = await activityModle.create(data)
    ctx.body = {

        code: 200,
        msg: "创建成功"
    }
})

// 省活跃度统计页面
//    获取全部省活跃度
router.get("/getActivityData", async (ctx) => {

    let resoult = await activityModle.find({})
    ctx.body = {
        code: 200,
        res: updata(resoult)
    }


})

// 查询省活跃度
router.post('/queryActivityData', async (ctx) => {
    let data = ctx.request.body
    console.log(data);
    let resoult = await activityModle.find(data)
    console.log(resoult);
    ctx.body = {
        code: 200,
        resoult: updata(resoult)
    }

})

//获取全部市活跃度
router.get('/getCityactivity', async (ctx) => {
    let resolut = await activityModle.find({})
    ctx.body = {
        code: 200,
        resolut: updata1(resolut)
    }

})

//查询市活跃度
router.post('/queryCityactivity', async (ctx) => {
    let data = ctx.request.body
    console.log(data);
    let resoult = await activityModle.find(data)
    console.log(resoult);
    ctx.body = {
        code: 200,
        resoult: updata1(resoult)
    }

})

//获取区活跃度
router.get("/getAreaactivity", async (ctx) => {
    let resoult = await activityModle.find({})

    ctx.body = {
        code: 200,
        resoult: areaData(resoult)
    }

})

//查询区活跃度
router.post('/queryAreaactivity', async (ctx) => {
    let data = ctx.request.body
    console.log(data);
    let resoult = await activityModle.find(data)
    console.log(resoult);
    ctx.body = {
        code: 200,
        resoult: areaData(resoult)
    }

})



module.exports = router
